// JavaScript Document
String.prototype.replaceAll = function(s1,s2){ 
	return this.replace(new RegExp(s1,"gm"),s2); 
}
var param={
	year:new Date().getFullYear(),
	month:new Date().getMonth()+1,
	ybType:1
}

function bindrq(){
	$("#rq").bind("click",function(e){
		var calendar=$("#calendarBox");
		if(calendar.css("height")=="0px"){
			calendar.css("height","70px");
		}else{
			calendar.css("height","0px");
		}
	}).html(" "+param.year+"年"+param.month+"月");
    $("#calendarBox").on("click","span",function(e){
    	var target=$(e.target);
        target.parent().children().removeClass("active");
        target.addClass("active");
        param[target.parent().attr("class")]=target.html()*1;
        $("#rq").html(" "+param.year+"年"+param.month+"月");
        loadData();
	});
    $("#thisMonth").on("click",function(e){
        param.year=new Date().getFullYear();
        param.month=new Date().getMonth()+1;
        $("#rq").html(" "+param.year+"年"+param.month+"月");
        $("#calendarBox .active").removeClass("active");
        $("#calendarBox .year #"+param.year).addClass("active");
        $("#calendarBox .month #"+param.month).addClass("active");
        loadData();
    });
}
function bindybType(){
	$("#ybType").bind("click",function(e){
		var active=$("#ybType .active");
		$("#ybType span").addClass("active");
		active.removeClass("active");
        param.ybType=$("#ybType .active").attr("value");
        loadData();
	});	
}
function bindFootButton(){
	var footButton=$(".footButton");
	footButton.bind("click",function(e){
		var target=$(e.target);
		footButton.removeClass("active");
		target.addClass("active");
        $(".tabPages").children(".active").removeClass("active");
        $(".tabPages").children().eq(target.index()).addClass("active");
	});
}
function bindChartTable(){
	$(".tableBox").bind("dblclick",function(e){
		var target=$(e.target);
		if(!target.hasClass("tableBox"))
			target=target.parents(".tableBox");
		target.css("transform","rotateY(-180deg)");
		target.prev().css("transform","rotateY(0deg)");
	});	
	$(".echartBox").bind("dblclick",function(e){
		var target=$(e.target);
		if(!target.hasClass("echartBox"))
			target=target.parents(".echartBox");
		target.css("transform","rotateY(180deg)");
		target.next().css("transform","rotateY(0deg)");
	});	
}
function loadCartTable(){
    $(".chartBox").bind("mouseover", function (e) {
        var box = $(e.target);
        if (!box.hasClass("chartBox")) {
            box = box.parents(".chartBox");
        }
        box.css("flex", "2");
        setTimeout(chartResize, 500);

    });
    $(".chartBox").bind("mouseleave", function (e) {
        var box = $(e.target);
        if (!box.hasClass("chartBox")) {
            box = box.parents(".chartBox");
        }
        box.css("flex", "1");
        setTimeout(chartResize, 500);
    });
}

function changeView(e,aPage,bPage) {
	var target=$(e);
	var Apage=$("#"+aPage);
    var Bpage=$("#"+bPage);

    if(Apage.hasClass("active")){
        Apage.removeClass("active");
        Bpage.addClass("active");
        target.html(Apage.attr("text"));
	}else{
        Bpage.removeClass("active");
        Apage.addClass("active");
        target.html(Bpage.attr("text"));
	}

}

function loadServerData(url, data, render, loadingParent) {
    showLoading($(loadingParent));
    $.ajax({
        url: url,
        data: {
            datas: JSON.stringify(data)
        },
        dataType: "json",
        success: function (ret) {
            hideLoading($(loadingParent));
            render(ret);
        },
        error: function (ret) {
            alert("发生错误");
            hideLoading($(loadingParent));
            console.debug(ret);
        }
    });
}

function showLoading(parent) {
    if (parent.find(".loading").length == 0)
        parent.append($("<div class='loading'></div>"));
}

function hideLoading(parent) {
    parent.find(".loading").fadeOut(300, function () {
        $(this).remove();
    });
}